// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // 弹窗
        layerPrefab: cc.Prefab,
        // 开启弹窗按钮
        button: cc.Node,
        // 显示的位置
        showOn: cc.Node,
        // 弹窗小图标
        layerThumb: cc.SpriteFrame,
        // 弹窗内容
        content: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_END , this.popupToggle, this)

    },

    start () {

    },

    // update (dt) {},

    // 弹窗关闭显示
    popupToggle(){
        if(this.layerPrefab !== null){
            // 使用给定的模板在场景中生成一个新节点
            var newLayer = cc.instantiate(this.layerPrefab),
                popup = newLayer.getChildByName('popup-main');

            // newLayer.children[1].addChild(this.content);

            // 修改弹窗的图片
            var spriteComponent = popup.getChildByName('thumb').getComponent(cc.Sprite);

            var main = popup.getChildByName('content');

            // console.log(main)
            this.node.addChild(this.content);
            
            // main = this.content;
            // popup.children[1].addChild(this.content);

            // popup.getChildByName('content').string = this.content;



            spriteComponent.spriteFrame = this.layerThumb;
            // console.log(spriteComponent);
            // 将新增的节点添加到 Canvas 节点下面
            this.showOn.addChild(newLayer);
        }

            // newLayer.setPosition(cc.v2(this.node.width/2 , this.node.height/2))
            // this.layer.active = !this.layer.active
    },
});
